package com.xjtu.project.controller.user;

import com.xjtu.project.common.utls.JsonResponse;
import com.xjtu.project.common.utls.SessionUtilsUser;
import com.xjtu.project.model.domain.Address;
import com.xjtu.project.service.AddressService;
import com.xjtu.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user")
public class AddressUserController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    @PostMapping("addressBook")
    public JsonResponse addAddress(@RequestBody Address address) {
        Integer b = addressService.addAdress(address);
        return JsonResponse.success(b);
    }

    @GetMapping("addressBook/list")
    public JsonResponse getAddressList() {
        List<Address> lists = addressService.getByUserId();
        return JsonResponse.success(lists);
    }

    @GetMapping("addressBook/default")
    public JsonResponse getDefaultAddress() {
        Address address = addressService.getOneMy();
        return JsonResponse.success(address);
    }

    @DeleteMapping("addressBook")
    public JsonResponse deleteAddress(Integer id) {
        boolean b = addressService.removeById(id);
        return JsonResponse.success(b);
    }

    @GetMapping("addressBook/{id}")
    public JsonResponse getAddress(@PathVariable("id") Integer id) {
        Address address = addressService.getById(id);
        return JsonResponse.success(address);
    }

    @PutMapping("addressBook")
    public JsonResponse updateAddress(@RequestBody Address address) {
        boolean b = addressService.updateByIdMy(address);
        return JsonResponse.success(b);
    }
    @PutMapping("addressBook/default")
    public JsonResponse setDefault(@RequestBody Address address) {
        Integer id=address.getId();
        addressService.setDefaultAddress(id);
        return JsonResponse.success(null);
    }
}
